package com.xn.myproject.ui;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xn.myproject.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 作者：tracyxia
 * 时间：11/8/15/21:34
 * 描述：
 * 版本：
 */
public class VideoCountActivity extends Activity implements View.OnClickListener {

    private TextView mVideoCount;
    private Button mCount;
    private TextView mTvCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_count);
        mCount = (Button)findViewById(R.id.btn_count);
        mVideoCount = (TextView) findViewById(R.id.tv_videoCount);
        mTvCount = (TextView) findViewById(R.id.tv_count);
        mCount.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String []projection = { MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.DATA};
        String orderBy = MediaStore.Video.Media.DISPLAY_NAME;
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        getContentProvider(uri, projection, orderBy);
    }

    /**
     * 获取ContentProvider
     * @param projection
     * @param orderBy
     */
    public void getContentProvider(Uri uri,String[] projection, String orderBy) {
        // TODO Auto-generated method stub
        List<HashMap<String, String>> listImage = new ArrayList<HashMap<String, String>>();
        Cursor cursor = getContentResolver().query(uri, projection, null,
                null, orderBy);
        if (null == cursor) {
            return;
        }
        while (cursor.moveToNext()) {
            HashMap<String, String> map = new HashMap<String, String>();
            for(int i=0;i<projection.length;i++){
                map.put(projection[i], cursor.getString(i));
                System.out.println(projection[i]+":"+cursor.getString(i));
            }
            listImage.add(map);
        }
        mVideoCount.setText(listImage.toString());
        mTvCount.setText("总数:"+listImage.size());
    }
}
